//package com.tiancheng.trade.settlement.util;
//
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.sensorsdata.analytics.javasdk.SensorsAnalytics;
//import com.tiancheng.trade.settlement.enums.*;
//import com.tiancheng.trade.settlement.exception.error.BusinessErrorInfoEnum;
//import com.tiancheng.trade.settlement.model.order.Orders;
//import com.tiancheng.trade.settlement.model.order.RefundOrdersDetail;
//import com.tiancheng.trade.settlement.model.order.SubOrder;
//import com.tiancheng.trade.settlement.service.order.IOrderService;
//import com.tiancheng.trade.settlement.service.order.IRefundService;
//import com.tiancheng.trade.settlement.service.order.ISubOrderService;
//import com.tiancheng.trade.settlement.util.queue.AbstractCustomQueueConsumer;
//import com.tiancheng.trade.settlement.util.queue.RedisDelayQueueEnum;
//import com.tiancheng.trade.settlement.util.queue.RedisDelayQueueUtil;
//import com.tiancheng.trade.settlement.vo.settlement.api.OrderActionVO;
//import com.tiancheng.trade.settlement.vo.settlement.api.SensorsDataVo;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Component
//public class SensorDataHandle extends AbstractCustomQueueConsumer {
//    @Resource
//    private SensorsAnalytics sensorsAnalytics;
//    @Autowired
//    private ObjectMapper objectMapper;
//    @Resource
//    private JacksonJsonUtil jacksonJsonUtil;
//    @Resource
//    private IOrderService orderService;
//    @Resource
//    private ISubOrderService subOrderService;
//    @Resource
//    private IRefundService refundService;
//    @Resource
//    private RedisDelayQueueUtil redisDelayQueueUtil;
//
//    public static final String EVENT_NAME = "all_Orderresult";
//
//
//    @Override
//    public void execute(Object params) {
//        try {
//            this.payDataHandle((Map<String, String>) params);
//        } catch (Exception e) {
//            log.error("订单消息推送神策异常,", e);
//        }
//    }
//
//    public void pushOrderMessage(Map<String, String> dataMap) {
//        redisDelayQueueUtil.addDelayQueue(dataMap, 5, TimeUnit.MINUTES, this.getQueueName());
//        log.info("消息存入延迟消息队列成功");
//    }
//
//    /**
//     * 支付结果信息推送到神策SDK
//     *
//     * @param dataMap
//     */
//
//    public void payDataHandle(Map<String, String> dataMap) throws Exception {
//        OrderActionVO orderActionVO = objectMapper.readValue(jacksonJsonUtil.toJson(dataMap.get("data")), new TypeReference<OrderActionVO>() {
//        });
//        if (null == orderActionVO || StringUtils.isEmpty(orderActionVO.getOrderId()) || StringUtils.isEmpty(orderActionVO.getActionType())) {
//            log.info("接收到订单信息异常");
//            return;
//        }
//        OrderActionTypeEnum actionTypeEnum = EnumUtil.getEnumByCode(OrderActionTypeEnum.class, orderActionVO.getActionType());
//        BusinessErrorInfoEnum.MESSAGE_ARGS_ERROR.assertNotNull(actionTypeEnum);
//        Orders order = orderService.findByOrderId(orderActionVO.getOrderId());
//        BusinessErrorInfoEnum.MESSAGE_NOT_EXIT_ORDER_ERROR.assertIsTrue(order != null && StringUtils.isNotEmpty(order.getOrderId()));
//        if (StringUtils.isNotEmpty(order.getBusinessType()) && order.getBusinessType().equals(BusinessTypeEnum.B2B.getCode())) {
//            log.info("B2B订单不需要推送");
//            return;
//        }
//        List<SubOrder> subOrders = null;
//        List<RefundOrdersDetail> refundOrdersDetailOrders = null;
//        if (CollectionUtils.isEmpty(orderActionVO.getSubOrderIds())) {
//            subOrders = subOrderService.findByOrderId(order.getOrderId());
//        } else {
//            subOrders = subOrderService.findByOrderIdAndSubOrderIds(order.getOrderId(), orderActionVO.getSubOrderIds());
//        }
//        BusinessErrorInfoEnum.MESSAGE_NOT_EXIT_ORDER_ERROR.assertIsTrue(!CollectionUtils.isEmpty(subOrders));
//        if (orderActionVO.getActionType().equals(OrderActionTypeEnum.refund.getCode())) {
//            BusinessErrorInfoEnum.MESSAGE_ARGS_ERROR.assertIsTrue(StringUtils.isNotEmpty(orderActionVO.getFlowId()));
//            refundOrdersDetailOrders = refundService.findByRefundFlowId(orderActionVO.getFlowId());
//            BusinessErrorInfoEnum.MESSAGE_NOT_EXIT_ORDER_ERROR.assertIsTrue(!CollectionUtils.isEmpty(refundOrdersDetailOrders));
//        }
//        for (SubOrder subOrder : subOrders) {
//            SensorsDataVo sensorsDataVo = new SensorsDataVo();
//            //BeanUtils.copyProperties(subOrder,sensorsDataVo);
//            sensorsDataVo.setOrderId(subOrder.getOrderId());
//            sensorsDataVo.setSubOrderId(subOrder.getSubOrderId());
//            sensorsDataVo.setOrderType(OrderSourceEnum.getDesc(order.getOrderSource()));
//            sensorsDataVo.setProductId(subOrder.getProductId());
//            sensorsDataVo.setProductName(subOrder.getProductName());
//            sensorsDataVo.setProductCount(subOrder.getProductCount());
//            sensorsDataVo.setPayAmount(AmountUtil.getYUAN(subOrder.getPayAmount()));
//            sensorsDataVo.setPayTime(order.getPayTime());
//            sensorsDataVo.setCoupon(subOrder.getIsCoupon());
//            sensorsDataVo.setPayScene(PaySceneEnum.getDesc(order.getPayScene()));
//            sensorsDataVo.setPayWay(PayWayEnum.getDesc(order.getPayWay()));
//            sensorsDataVo.setOrderScene(OrderSceneEnum.getDesc(order.getOrderScene()));
//            if (actionTypeEnum.getCode().equals(OrderActionTypeEnum.create.getCode())) {
//                // action——创建订单
//                sensorsDataVo.setOrderStatus(OrderStatusEnum.waiting_pay.getCode());
//                sensorsDataVo.setPayStatus(OrderPayStatusEnum.un_pay.getCode());
//            } else if (actionTypeEnum.getCode().equals(OrderActionTypeEnum.close.getCode())) {
//                // action——取消订单
//                sensorsDataVo.setOrderStatus(OrderStatusEnum.cancelled.getCode());
//                sensorsDataVo.setPayStatus(OrderPayStatusEnum.un_pay.getCode());
//            } else {
//                sensorsDataVo.setOrderStatus(order.getOrderStatus());
//                sensorsDataVo.setPayStatus(order.getPayStatus());
//            }
//            if (actionTypeEnum.getCode().equals(OrderActionTypeEnum.refund.getCode())) {
//                // 退款消息，需要查询退款信息
//                List<RefundOrdersDetail> refundOrdersDetails = refundOrdersDetailOrders.stream().filter(refundOrdersDetail -> refundOrdersDetail.getSubOrderId().equals(subOrder.getSubOrderId())).collect(Collectors.toList());
//                sensorsDataVo.setRefundAmount(AmountUtil.getYUAN(refundOrdersDetails.get(0).getRefundAmount()));
//                sensorsDataVo.setRefundStatus(refundOrdersDetails.get(0).getRefundStatus());
//            } else {
//                sensorsDataVo.setRefundAmount(AmountUtil.getYUAN(0));
//            }
//            if (null != subOrder.getDiscount()) {
//                sensorsDataVo.setDiscount(AmountUtil.getYUAN(subOrder.getDiscount()));
//            } else {
//                sensorsDataVo.setDiscount(AmountUtil.getYUAN(0));
//            }
//            sensorsDataVo.setMerchantName(subOrder.getMerchantName());
//            sensorsDataVo.setOrderChannel(order.getOrderChannel());
//            String propertiesStr = jacksonJsonUtil.toJson(sensorsDataVo);
//            Map<String, Object> properties = jacksonJsonUtil.fromJson(propertiesStr, Map.class);
//            sensorsAnalytics.track(order.getUid(), true, EVENT_NAME, properties);
//            log.info("推送数据到神策成功：{}", jacksonJsonUtil.toJson(properties));
//        }
//
//    }
//
//
//    @Override
//    public String getQueueName() {
//        return RedisDelayQueueEnum.TRADE_ORDER_PUSH_DELAY_QUEUE.getCode();
//    }
//
//    /**
//     * 轮训时间间隔，单位：s
//     *
//     * @return
//     */
//    @Override
//    public Integer getPullIntervalSeconds() {
//        return 1;
//    }
//
//}
